<?php
/**
 *	FILENAME: 		/server_app/student/registered_terms.php
 *	DESCRIPTION:	This backend script produces a JSON response representing a listing of the term names and IDs for which the student has registered.
 *	AUTHOR:			Casey Burkhardt 
 *	VERSION:		1.1.0
 *	LAST MODIFIED:	12/3/2010
 **/

require_once($_SERVER['DOCUMENT_ROOT'] . "/config/environment.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/libraries/functions.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/libraries/logging.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/libraries/novasis.php");

enforceSSL();
setRequestParameters();

$studentID = $_PARAMETERS["studentID"];
$pin = $_PARAMETERS["pin"];

if ((strlen($studentID) == 0) || (strlen($pin) == 0)) {
    $response["response_type"] = "error";
    $response["error_desc"] = "Student ID or PIN not Provided.";
    logWarning("No user data supplied");
    echo json_encode($response);
    die();
} else {
    // Create a NOVASIS session with the student ID and pin provided.
    $sessionData = getNOVASISSession($studentID, $pin);

    // Validate the session.
    if ($sessionData === FALSE) {
        $response["response_type"] = "error";
        $response["error_desc"] = "Invalid NOVASIS login information provided.";
        logWarning("User failed NOVASIS authentication");
        echo json_encode($response);
        die();
    } else {
        $termData = getTermIDsWithRegistrationData($sessionData);
        // Validate the data result.
        if ($termData === FALSE) {
            $response["response_type"] = "error";
            $response["error_desc"] = "Unable to obtain term data.";
            logError("Unable to obtain term data");
            echo json_encode($response);
            die();
        } else {
            // Encode and return the course data.
            $response["response_type"] = "success";
            $response["term_data"] = $termData;
            logAction("Returned NOVASIS term data");
            echo json_encode($response);
            die();
        }
    }
}
?>
